//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    a1src: '../../images/a4.png',
    a2src: '../../images/gouwuche.png',
    a3src: '../../images/lianxiwomen.png',
    a4src: '../../images/xiangce.png',
    laba: '../../images/laba.png',

    product_list: [],
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    showModalStatus: false,
    animationData: '',
    phoneNumber: "",
  },
  // 拨打电话
  bodadianhua() {
    wx.makePhoneCall({
      phoneNumber: this.data.phoneNumber //客服电话
    })
  },

  onLoad: function(ops) {},
  login: function() {
    let that = this;
    let token = wx.getStorageSync('token');
    // 获取登录token
    if (token) {
      wx.request({
        url: app.globalData.hostname + ":" + app.globalData.port + '/user/check-token',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          token: token
        },
        success: function(res) {
          if (res.data.code != 0) {
            wx.removeStorageSync('token')
          } else {
            wx.getUserInfo({
              success: res => {
                that.setData({
                  hasloginon: true,
                })
                app.globalData.userInfo = res.userInfo
                app.globalData.wxid = wx.getStorageSync('wxid')
                // app.globalData.odoo.connect(function(res, uid) {
                //   var inParams = [];
                //   inParams.push([
                //     ['wechat_id', '=', app.globalData.wxid]
                //   ]);
                //   inParams.push(['name', 'id', ]);
                //   app.globalData.odoo.execute_kw('res.partner', 'search_read', inParams, function(err, value) {
                //     if (value[0]) {
                //       app.globalData.UserId = value[0].id
                //     }
                //   });
                // });
              }
            })
          }
        },
      })
    }
    //获取服务热线和联系我们电话
    wx.request({
      url: app.globalData.hostname + ":" + app.globalData.port + '/index/phone',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function(res) {
        that.setData({
          phoneNumber: res.data.phone,
        })
      }
    })

    wx.request({
      url: app.globalData.hostname + ":" + app.globalData.port + '/index/pic',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function(res) {
        // 将字符串转成对象
        res.data.pic_list = JSON.parse(res.data.pic_list)
        //遍历对象提取图片转成base64 并将传来的二进制开头b去掉
        for (var i = 0; i < res.data.pic_list.length; i++) {
          var array = wx.base64ToArrayBuffer(res.data.pic_list[i]);
          var base64 = wx.arrayBufferToBase64(array);
          res.data.pic_list[i] = "data:image/png;base64," + base64.substr(1);
        }
        that.setData({
          imgUrls: res.data.pic_list
        })
      }
    })
    // app.globalData.odoo.connect(function(res, uid) {
    //   var inParams = [];
    //   inParams.push([
    //   ]);
    //   inParams.push(['datas']);
    //   app.globalData.odoo.execute_kw('ir.attachment', 'search_read', inParams, function(err, value) {
    //     if (value[0]) {
    //       // app.globalData.UserId = value[0].id
    //       console.log(value[0])
    //     }
    //   });
    // }
    // );


    //获取推荐车辆
    wx.request({
      url: app.globalData.hostname + ":" + app.globalData.port + '/index/product',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function(res) {
        for (var i = 0; i < res.data.product_list.length; i++){
          var array = wx.base64ToArrayBuffer(res.data.product_list[i].pic);
          var base64 = wx.arrayBufferToBase64(array);
          res.data.product_list[i].pic = "data:image/png;base64," + base64.substr(1);
        }
        that.setData({
          product_list: res.data.product_list
        })
      }
    })

  },
  onShow: function() {
    var that = this
    // if (that.data.product_list.pic) {
    //   var pic = wx.base64ToArrayBuffer(that.data.product_list.pic)
    //   var b64_pic = wx.arrayBufferToBase64(pic)
    //   that.data.product_list.pic = "data:image/png;base64," + b64_pic
    //   console.log(that.data.product_list.pic)
    // }

    that.login()
  },
  //事件处理函数
  quote: function() {
    var that = this
    if (!that.data.hasloginon) {
      wx.showModal({
        title: '提示',
        content: '请先授权登录',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            setTimeout(function() {
              wx.switchTab({
                url: "/pages/user/index"
              })
            }, 500);
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/index/index'
      })
    }

  },

  showModal: function() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function() {
    this.setData({
      showModalStatus: false,
    })
  },
  // 跳转到详情
  car_details: function(e) {
    wx.navigateTo({
      url: '/pages/car_details/car_details?id=' + e.currentTarget.id,
    })
  },
  //跳转到 车辆相册
  car_img: function() {
    wx.navigateTo({
      url: '/pages/car_img/car_img',
    })
  },
  //页面跳转
  jump_alllist: function() {
    wx.switchTab({
      url: '/pages/select_car/index'
    })
  },

  //预览图片
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imgUrls // 需要预览的图片http链接列表  
    })
  },

  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '租车小程序',
      path: '/pages/index/index',
      success: function(res) {
        // 转发成功
        console.log("转发成功")
      },
      fail: function(res) {
        // 转发失败
        onsole.log("转发失败")
      }
    }
  }
})